#include "../lvgl/lvgl.h"

/* ------------------ 全局结构体 ------------------ */
typedef struct {
    lv_obj_t *kb;
    lv_obj_t *login_btn;
} ui_ctx_t;

static ui_ctx_t g_ui;      /* 全局实例，所有回调都能拿到 */
/* ------------------ 回调 ------------------ */
void ta_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    ui_ctx_t *ui = (ui_ctx_t *)lv_event_get_user_data(e);

    if(code == LV_EVENT_FOCUSED) {
        lv_keyboard_set_textarea(ui->kb, ta);
        lv_obj_clear_flag(ui->kb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(ui->login_btn, LV_OBJ_FLAG_HIDDEN);
    }
    if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(ui->kb, NULL);
        lv_obj_add_flag(ui->kb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(ui->login_btn, LV_OBJ_FLAG_HIDDEN);
    }
}

void login_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        show_msgbox();
    }
}

/* ------------------ 消息框 ------------------ */
static void msgbox_event_cb(lv_event_t *e)
{
    lv_obj_t *obj = lv_event_get_current_target(e);
    const char *btn_text = lv_msgbox_get_active_btn_text(obj);

    LV_LOG_USER("Button %s clicked", btn_text);

    if(strcmp(btn_text, "Apply") == 0) {
        LV_LOG_USER("Apply button pressed - perform login action");
    }                                                                                                    
    lv_obj_del(obj);
}

void show_msgbox(void)
{
    static const char *btns[] = {"Apply", "refuse", ""};
    lv_obj_t *mbox1 = lv_msgbox_create(NULL, "fuck you",
                                       "This is a message box with two buttons.",
                                       btns, true);
    lv_obj_add_event_cb(mbox1, msgbox_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_center(mbox1);
}

/* ------------------ 主函数 ------------------ */
void Keyscan(void)
{   
    static lv_ft_info_t info;
    info.name = "/userdata/zjy/simhei.ttf"; // 指定字体的文件路径
    info.weight = 16;  // 设置字体宽度
    info.style = FT_FONT_STYLE_NORMAL;  // 字体的风格
    info.mem = NULL; 
    if(!lv_ft_font_init(&info)) {
        LV_LOG_ERROR("create failed.");
    }

    /*Create style with the new font*/
    static lv_style_t style; // 创建一个新的字体风格
    lv_style_init(&style); // 舒适化风格
    lv_style_set_text_font(&style, info.font); //设置该风格为指定的字体风格
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER); // 设置该风格字体的对齐方式
    lv_style_set_pad_left(&style, 0);   /* 把左内边距设 0 */

    /*----------------------------------------*/
    
    /* 初始化结构体成员 */
    g_ui.kb        = lv_keyboard_create(lv_scr_act());
    g_ui.login_btn = lv_btn_create(lv_scr_act());
    lv_obj_add_flag(g_ui.kb, LV_OBJ_FLAG_HIDDEN);

    /* 用户输入框 */
    lv_obj_t *ta_user = lv_textarea_create(lv_scr_act());
    lv_obj_align(ta_user, LV_ALIGN_CENTER, 0, -60);
    lv_obj_add_style(ta_user, &style, 0);
    lv_textarea_set_placeholder_text(ta_user, "用户");
    
    lv_obj_add_event_cb(ta_user, ta_event_cb, LV_EVENT_ALL, &g_ui);
    lv_obj_set_size(ta_user, 200, 45);
    
    /* 密码输入框 */
    lv_obj_t *ta_passwd = lv_textarea_create(lv_scr_act());
    lv_obj_align(ta_passwd, LV_ALIGN_CENTER, 0, 10);
    lv_obj_add_style(ta_passwd, &style, 0);
    lv_textarea_set_placeholder_text(ta_passwd, "密码");
    
    lv_obj_add_event_cb(ta_passwd, ta_event_cb, LV_EVENT_ALL, &g_ui);
    lv_obj_set_size(ta_passwd, 200, 45);

    /* 登录按钮 */
    lv_obj_align(g_ui.login_btn, LV_ALIGN_CENTER, 0, 80);
    lv_obj_add_event_cb(g_ui.login_btn, login_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_size(g_ui.login_btn, 50, 25);

    lv_obj_t *label = lv_label_create(g_ui.login_btn);
    lv_obj_add_style(label, &style, 0);
    lv_label_set_text(label, "登录");
    lv_obj_center(label);
}